package com.devgroupvn.guinness;

import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class GetGuiness {
	
	public ArrayList<PostGuiness> lastestnews;
	
	public final int pagesize = 15;
	public int current = 0; // page index
	
	public GetGuiness() {

		lastestnews = new ArrayList<PostGuiness>();
	}
	
	public String searchRecord(String term) {
		try {
			/*
			Document doc = Jsoup.connect(url).timeout(30000).get();
			Elements list = doc.select("li");
			for (Element item : list) {
				PostGuiness post = new PostGuiness();
				
				post.title = item.select("h3").first().text();
				post.description = "";
				post.image = "http://www.guinnessworldrecords.com" + item.select("img").first().attr("src");
				post.url = "http://www.guinnessworldrecords.com/" + item.select("a").last().attr("href");
				
				searchrecord.add(post);
			}
			*/
			String url = "http://www.guinnessworldrecords.com/base/rest/GetSearchResults/" + term + "/" + (current*pagesize) + "/" + pagesize;;
			Document doc = Jsoup.connect(url).timeout(30000).get();
			String json = doc.select("body").first().html();
			json = json.replaceAll("&quot;", "\"");
			return json;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "Error";
	}
	
	public String getListJSON(String url) {
		try {
			String rel = "";
			
			if (url.equals("speed")) {
				rel = "1109";
			} else if (url.equals("size")) {
				rel = "1107";
			} else if (url.equals("dedication")) {
				rel = "1108";
			} else if (url.equals("extra-ordinary")) {
				rel = "1110";
			}
			
			url = "http://www.guinnessworldrecords.com/base/rest/LoadRecords/" + rel+ "/" + (current*pagesize) + "/" + pagesize;
			
			Document doc = Jsoup.connect(url).timeout(30000).get();
			String json = doc.select("body").first().html();
			json = json.replaceAll("&quot;", "\"");
			return json;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "Error";
	}
	
	public void getLastestNews() {
		String url = "http://www.guinnessworldrecords.com/explore-records/";
		try {
			Document doc = Jsoup.connect(url).timeout(30000).get();
			Element ul = doc.select(".newsList").last();
			Elements li = ul.select("li");
			for (Element item : li) {
				PostGuiness post = new PostGuiness();
				
				post.title = item.select("h3").first().text();
				post.description = item.select("p").first().text();
				post.url = "http://www.guinnessworldrecords.com" + item.select("a").first().attr("href");
				post.image = "http://www.guinnessworldrecords.com" + item.select("img").first().attr("src");
				
				lastestnews.add(post);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getPostDetail(String url) {
		String html = "";
		try {
			Document doc = Jsoup.connect(url).timeout(30000).get();
			html = doc.select("#masthead").first().html();
			html = html + doc.select("article").first().html();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return html;
	}
	
}
